package com.helasasa.maxfunds.lineofcredit.cashloan.ghana.screen.tabrepay.subview

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import com.helasasa.maxfunds.lineofcredit.cashloan.R
import java.math.BigDecimal
import java.math.RoundingMode
import kotlin.math.ceil
import kotlin.math.max
import kotlin.math.min

interface ITabRepayGhanaSliderListener {
    fun onProcess(process: Float, curValue: Int)
}

class TabRepayGhanaSlider : RelativeLayout {

    private lateinit var backgroundBar: View
    private lateinit var processBar: View
    private lateinit var dot: ImageView

    private var minValue: Int = 1
    private var maxValue: Int = 1
    private var stepValue: Int = 100

    /** maxValue-minValue */
    private var edgeValue: Int = 0

    /** edgeValue / stepValue */
    private var divValue: Int = 0

    private var listener: ITabRepayGhanaSliderListener? = null

    constructor(context: Context) : super(context) {
        initializeView(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initializeView(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initializeView(context)
    }

    private fun initializeView(context: Context) {
        inflate(context, R.layout.view_ghana_tab_repay_slider, this)
        backgroundBar = findViewById(R.id.backgroundBar)
        processBar = findViewById(R.id.processBar)
        dot = findViewById(R.id.dot)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                val startX = event.x
                updateDotX(startX)
            }

            MotionEvent.ACTION_MOVE -> {
                val endX = event.x
                updateDotX(endX)
            }

            MotionEvent.ACTION_UP -> {
                // 处理手指抬起事件，如果需要的话
            }
        }
        return true
    }

    private fun updateDotX(moveX: Float) {
        val barLayoutParams = processBar.layoutParams as RelativeLayout.LayoutParams
        val dotLayoutParams = dot.layoutParams as RelativeLayout.LayoutParams
        val dotWidth = dot.width
        if (moveX < 0) {
            dotLayoutParams.marginStart = 0
            dot.layoutParams = dotLayoutParams

            barLayoutParams.width = moveX.toInt()
            processBar.layoutParams = barLayoutParams
            invalidate()

            // 回调
            this.listener?.apply {
                onProcess(0F, minValue)
            }

        } else if (moveX >= width) {
            dotLayoutParams.marginStart = (width - dotWidth)
            dot.layoutParams = dotLayoutParams

            barLayoutParams.width = width
            processBar.layoutParams = barLayoutParams
            invalidate()

            // 回调
            this.listener?.apply {
                onProcess(1F, maxValue)
            }

        } else {
            val edge = (moveX - dotWidth).toInt()
            if (edge < 0) {
                dotLayoutParams.marginStart = 0
            } else {
                dotLayoutParams.marginStart = edge
            }
            dot.layoutParams = dotLayoutParams

            barLayoutParams.width = moveX.toInt()
            processBar.layoutParams = barLayoutParams
            invalidate()

            // 回调
            val processEdge = (moveX / width).toString()
            val result = BigDecimal(processEdge).setScale(1, RoundingMode.HALF_UP)
            val resultF = result.toFloat()
//            println("====processEdge: $resultF")
            if (resultF >= 1) {
                this.listener?.apply {
                    onProcess(resultF, maxValue)
                }
            } else {
                var curValue = maxValue
                if ((edgeValue > 0) and (edgeValue >= stepValue)) {
                    // 当前移动距离
                    val curMoveInt = moveX.toInt()
                    // 每一步距离
                    val eachStepInt = ceil((width / divValue.toFloat()).toDouble()).toInt()
                    // 移动倍数
                    val moveScaleInt = curMoveInt / eachStepInt
                    // 真正的移动值
                    curValue = minValue + stepValue * moveScaleInt
                    curValue = max(curValue, minValue)
                    curValue = min(curValue, maxValue)
//                    println("curMoveInt:$curMoveInt eachStepInt:$eachStepInt moveScaleInt:$moveScaleInt curValue:$curValue")
                }
                this.listener?.apply {
                    onProcess(resultF, curValue)
                }
            }
        }
    }

    fun setProcessChangedListener(listener: ITabRepayGhanaSliderListener) {
        this.listener = listener
    }

    fun setStepConfig(min: Int, max: Int, step: Int = 100) {
        this.minValue = min
        this.maxValue = max
        this.stepValue = step

        this.edgeValue = this.maxValue - this.minValue
        if (this.stepValue > 0) {
            this.divValue = this.edgeValue / this.stepValue
        }
    }

    fun setProcess(process: Float) {
        if (process >= 1) {
            updateDotX(40000F)
        }
    }

    fun reset(){
        val barLayoutParams = processBar.layoutParams as RelativeLayout.LayoutParams
        val dotLayoutParams = dot.layoutParams as RelativeLayout.LayoutParams

        dotLayoutParams.marginStart = 0
        dot.layoutParams = dotLayoutParams

        barLayoutParams.width = 0
        processBar.layoutParams = barLayoutParams

        invalidate()
    }
}
